//
//  ViewController.m
//  JZRJAPP
//
//  Created by mac on 16/5/11.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "HomeController.h"
#import "AutoChangerController.h"
#define kIMGCount 5

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scroll;
@property (nonatomic,strong) UIPageControl *page;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor redColor];
    
    /*
     *创建并添加UIScrollView
     */
    _scroll=[[UIScrollView alloc]init];
    _scroll.frame=self.view.bounds;
    [self.view addSubview:_scroll];
    CGFloat width=_scroll.frame.size.width;
    CGFloat height=_scroll.frame.size.height;
    for (int i=1; i<kIMGCount+1; i++) {
        UIImageView* imageView=[[UIImageView alloc]init];
        CGFloat imageX=(i-1)*width;
        CGFloat imageY=0;
        imageView.frame=CGRectMake(imageX, imageY, width, height);
        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
        [_scroll addSubview:imageView];
        /**
         *在最后一张图片添加进入主页的按钮
         */
        if (i==kIMGCount){
            [self setupLastImageView:imageView];
        }
    }
    /*
     *添加UIPageControl，并设置相关属性
     */
    _page=[[UIPageControl alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/6 *5, self.view.frame.size.width, 30)];
    _page.numberOfPages=kIMGCount;
    _page.pageIndicatorTintColor=[UIColor redColor];
    _page.currentPageIndicatorTintColor=[UIColor blueColor];
    [self.view addSubview:_page];
    
    /*
     *设置UIScrollView属性
     */
#warning UIScrollView必须指明的一个属性，没有指明，只显示第一张图片，而且不能交互，拖动图片
    _scroll.contentSize=CGSizeMake(kIMGCount*width, 0);
    _scroll.pagingEnabled=YES;
    _scroll.showsHorizontalScrollIndicator=NO;
    _scroll.showsVerticalScrollIndicator=NO;
    _scroll.bounces=NO;
    _scroll.delegate=self;
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)setupLastImageView:(UIImageView*)imageView
{
#warning 在imageView上添加按钮，需要开启用户交互功能，UIImageView这个功能默认关闭
    
    imageView.userInteractionEnabled=YES;
    
    UIButton* btn=[[UIButton alloc]init];
    btn.frame=CGRectMake(0, self.view.frame.size.height/4 *3, self.view.frame.size.width, 75);
    btn.layer.cornerRadius=10;
    [btn setTitle:@"开始你的风景之旅" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(jumpMainPage) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:btn];
}
#pragma mark-UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset=scrollView.contentOffset;
    CGFloat offsetX=offset.x;
    int pageNum=(offsetX+0.5*_scroll.frame.size.width)/_scroll.frame.size.width;
    _page.currentPage=pageNum;
}
#pragma mark-btn的点击监听事件
-(void)jumpMainPage
{
    /**
     *拿到窗口的keyWindow，设置根控制器
     */
    
    // UIStoryBoard 找到视图控制器
    // 首页
    UINavigationController *NVCHome = [[UINavigationController alloc] initWithRootViewController:[[HomeController alloc] init]];
    NVCHome.navigationBar.translucent = NO;
    
    NVCHome.tabBarItem.image = [UIImage imageNamed:@"index_off"];
    NVCHome.tabBarItem.selectedImage = [UIImage imageNamed:@"index_on"];
    NVCHome.tabBarItem.title = @"首页";
    // 自动
    UINavigationController *NVCAuto = [[UINavigationController alloc] initWithRootViewController:[[AutoChangerController alloc] init]];
    NVCAuto.navigationBar.translucent = NO;
    
    NVCAuto.tabBarItem.image = [UIImage imageNamed:@"self_off"];
    NVCAuto.tabBarItem.selectedImage = [UIImage imageNamed:@"self_on"];
    NVCAuto.tabBarItem.title = @"自动";
    
    // 消息
    UINavigationController *NVCMessage = [[UINavigationController alloc] initWithRootViewController:[[AutoChangerController alloc] init]];
    NVCMessage.navigationBar.translucent = NO;
    
    NVCMessage.tabBarItem.image = [UIImage imageNamed:@"message_off"];
    NVCMessage.tabBarItem.selectedImage = [UIImage imageNamed:@"message_on"];
    NVCMessage.tabBarItem.title = @"消息";
    UITabBarController *tabbar = [[UITabBarController alloc] init];
    tabbar.viewControllers = @[NVCHome,NVCAuto,NVCMessage];
    //navigationbar背景颜色
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    // 普通状态
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor grayColor], NSForegroundColorAttributeName,[UIFont systemFontOfSize:15],NSFontAttributeName,
                                                       nil] forState:UIControlStateNormal];
    UIColor *titleHighlightedColor = [UIColor redColor];
    
    //高亮状态
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, NSForegroundColorAttributeName,[UIFont systemFontOfSize:15],NSFontAttributeName,
                                                       nil] forState:UIControlStateSelected];
    
    
    
    
    
    
    
    [self presentViewController:tabbar animated:YES completion:nil];}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
